package com.cal.utms.model.maintenance;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cal.utms.dao.maintenance.MasterDataDaoImpl;
import com.cal.utms.dto.maintenance.DealerDto;
import com.cal.utms.dto.maintenance.DelevaryMethodDto;
import com.cal.utms.dto.maintenance.SettlementMethodDto;
import com.cal.utms.entity.Dealer;
import com.cal.utms.entity.DelevaryMethod;
import com.cal.utms.entity.SettlementMethod;

public class MasterDataModel {

	private MasterDataDaoImpl daoImpl = new MasterDataDaoImpl();

	public List<SettlementMethodDto> getSettlementMethode() {
		List<SettlementMethodDto> dtos = new ArrayList<SettlementMethodDto>();
		List<SettlementMethod> list = daoImpl.getSettlementMethodes();
		if (list.size() > 0) {
			for (SettlementMethod settlementMethod : list) {
				SettlementMethodDto dto = new SettlementMethodDto();
				dto.setId(settlementMethod.getId());
				dto.setCode(settlementMethod.getCode());
				dto.setDescription(settlementMethod.getDescription());
				dto.setStatus(settlementMethod.isStatus());
				dtos.add(dto);
			}
		} else {
			dtos = null;
		}
		return dtos;
	}

	public List<DelevaryMethodDto> getDelevaryMethods() {
		List<DelevaryMethod> delevaryMethods = daoImpl.getDelevaryMethods();
		List<DelevaryMethodDto> delevaryMethodDtos = new ArrayList<DelevaryMethodDto>();
		if (delevaryMethods.size() > 0) {
			for (DelevaryMethod delevaryMethod : delevaryMethods) {
				DelevaryMethodDto dto = new DelevaryMethodDto();
				dto.setId(delevaryMethod.getId());
				dto.setCode(delevaryMethod.getCode());
				dto.setDescription(delevaryMethod.getDescription());
				dto.setStatus(delevaryMethod.isStatus());
				delevaryMethodDtos.add(dto);
			}
		} else {
			delevaryMethodDtos = null;
		}
		return delevaryMethodDtos;
	}

	public List<DealerDto> getDealers() {
		List<Dealer> dealers = daoImpl.getDealers();
		List<DealerDto> dealerDtos = new ArrayList<DealerDto>();
		if (dealers.size() > 0) {
			for (Dealer dealer : dealers) {
				DealerDto dealerDto = new DealerDto();
				dealerDto.setId(dealer.getId());
				dealerDto.setCode(dealer.getCode());
				dealerDto.setName(dealer.getName());
				dealerDto.setStatus(dealer.isStatus());
				dealerDtos.add(dealerDto);
			}
		} else {
			dealerDtos = null;
		}
		return dealerDtos;
	}

	public void saveSettlementMethod(String settlementmethods, SettlementMethodDto methodDto) {
		List<SettlementMethod> methods = new ArrayList<SettlementMethod>();
		if (settlementmethods != null) {
			settlementmethods = "{ \"settlementmethods\" : ".concat(settlementmethods) + " }";
			try {
				JSONObject jsonObject = new JSONObject(settlementmethods);
				JSONArray jArray = jsonObject.getJSONArray("settlementmethods");
				for (int i = 0; i < jArray.length(); i++) {
					JSONObject jObj = jArray.getJSONObject(i);
					SettlementMethod method = new SettlementMethod();
					if (jObj.getInt("id") != 0) {
						method.setId(jObj.getInt("id"));
					}
					method.setCode(jObj.getString("code"));
					method.setDescription(jObj.getString("description"));
					method.setStatus(jObj.getBoolean("status"));
					methods.add(method);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

			Object object = daoImpl.saveSettlement(methods);
			methodDto.setSuccess(true);
			methodDto.setResultMsg("SUCCESS");
		}
	}

	public void saveDeliveryMethod(String deliverymethods, DelevaryMethodDto delevaryMethodDto) {
		List<DelevaryMethod> methods = new ArrayList<DelevaryMethod>();
		if (deliverymethods != null) {
			deliverymethods = "{ \"deliverymethods\" : ".concat(deliverymethods) + " }";
			try {
				JSONObject jsonObject = new JSONObject(deliverymethods);
				JSONArray jArray = jsonObject.getJSONArray("deliverymethods");
				for (int i = 0; i < jArray.length(); i++) {
					JSONObject jObj = jArray.getJSONObject(i);
					DelevaryMethod method = new DelevaryMethod();
					if (jObj.getInt("id") != 0) {
						method.setId(jObj.getInt("id"));
					}
					method.setCode(jObj.getString("code"));
					method.setDescription(jObj.getString("description"));
					method.setStatus(jObj.getBoolean("status"));
					methods.add(method);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

			Object object = daoImpl.saveDeliveryMethod(methods);
			delevaryMethodDto.setSuccess(true);
			delevaryMethodDto.setResultMsg("SUCCESS");
		}
	}

	public void saveDealers(DealerDto dealerDto, String dealers) {
		List<Dealer> methods = new ArrayList<Dealer>();
		if (dealers != null) {
			dealers = "{ \"dealers\" : ".concat(dealers) + " }";
			try {
				JSONObject jsonObject = new JSONObject(dealers);
				JSONArray jArray = jsonObject.getJSONArray("dealers");
				for (int i = 0; i < jArray.length(); i++) {
					JSONObject jObj = jArray.getJSONObject(i);
					Dealer dealer = new Dealer();
					if (jObj.getInt("id") != 0) {
						dealer.setId(jObj.getInt("id"));
					}
					dealer.setCode(jObj.getString("code"));
					dealer.setName(jObj.getString("name"));
					dealer.setStatus(jObj.getBoolean("status"));
					methods.add(dealer);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

			Object object = daoImpl.saveDealers(methods);
			dealerDto.setSuccess(true);
			dealerDto.setResultMsg("SUCCESS");
		}
	}

}
